﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;
using System.Management;
using System.Xml.Linq;
using CameraWin;

namespace ABELGX
{
    public class Driver
    {
        public Car Car { get; set; }
        List<Port> Ports = new List<Port>();
        List<Trans> Trans = new List<Trans>();
        public Driver()
        {
            CHCNetSDK.NET_DVR_Init();
            Car = new Car(Config.Parameter);
            foreach (var set in Config.Parameter.Ports)
            {
                var port = new Port(set);
                port.DataReceive += Port_DataReceive;
                Ports.Add(port);
            }
            foreach(var set in Config.Parameter.Trans)
            {
                Trans.Add(new Trans(set));
            }

            Task.Run(() => controlWork());
        }

        private void Port_DataReceive(object sender, EventArgs e)
        {
            //byte[] test1 = new byte[] { 0xAA ,0x55, 0x13, 0x2A, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x4D, 0x00, 0x4D, 0x00, 0x15 };
            //Array.Copy(test1, 0, buffer, buffer_end, test1.Length);
            //buffer_start = 0;
            //buffer_end = test1.Length;
            Port port = (Port)sender;
            int payload_len = 0;
            while (port.BufferStart < port.BufferEnd - 4)
            {
                if (port.Buffer[port.BufferStart] != 0xAA || port.Buffer[port.BufferStart + 1] != 0x55)
                {
                    port.BufferStart++;
                    continue;
                }
                payload_len = port.Buffer[port.BufferStart + 2] + 4;
                if (port.BufferStart + payload_len > port.BufferEnd)
                {
                    break;
                }
                byte xor = port.Buffer[port.BufferStart + 2];
                for (int i = 3; i < payload_len - 1; i++)
                {
                    xor ^= port.Buffer[port.BufferStart + i];
                }
                if (port.Buffer[port.BufferStart + payload_len - 1] != xor)
                {
                    port.BufferStart++;
                    break;
                }
                Car.ProcessData(port.Buffer, port.BufferStart + 3);
                port.BufferStart += payload_len;
            }
            if (port.BufferEnd >= 7000)
            {
                port.Clear();
            }
        }

        public void Stop()
        {
            Car.Run = false;
            foreach(var port in Ports)
            {
                port.Close();
            }
            foreach (var trans in Trans)
            {
                trans.Close();
            }
        }
        void controlWork()
        {
            while (Car.Run)
            {
                try
                {
                    byte[] cmd = Car.GetCmd();
                    foreach(var port in Ports)
                    {
                        try
                        {
                            port.Send(cmd);
                        }
                        catch { }
                    }
                }
                catch
                {

                }
                Thread.Sleep(20);
            }
        }
    }
}
